<?php
    // 关联定义
    namespace app\index\model;
    use think\Model;
    class User extends Model
    {
        // 开启自动写入时间戳
        protected $autoWriteTimestamp = True;

        // 定义自动完成的属性
        protected $insert = ['status' => 1];

        // 定义一对一关联方法
        public function profile()
        {
            // 用户has one档案关联
            return $this->hasOne('Profile');
        }
        
        // status属性读取器
        protected function getStatusAttr($value)
        {
            $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
            return $status[$value];
        }

        // 定义一对多关联
        public function books()
        {
            return $this->hasMany('Book');
        }

        // 定义多对多关联
        public function roles()
        {
            return $this->belongsToMany('Role','access');
        }

        protected $type = [
            'birthday' => 'timestamp:Y-m-d',
        ];

        // 定义user_birthday读取器
        public function getBirthdayAttr($value)
        {
            return $value;
        }

        /* protected $type = [
            'birthday' => 'timestamp:Y/m/d',
        ];
        // 定义自动完成的属性
        protected $insert = ['status' => 1];
        // birthday读取器
        public function getBirthdayAttr($birthday)
        {
            return $birthday;
        }
        // user_birthday读取器
        public function getUserBirthdayAttr($value,$data)
        {
            return date('Y-m-d',$data['birthday']);
        }
        // user_birthday修改器
        public function setBirthdayAttr($value)
        {
            return strtotime($value);
        }
        
        // status属性修改器
        public function setStatusAttr($value,$data)
        {
            return '流年' == $data['nickname'] ? 1 : 2;
        }
        // status读取器
        public function getStatusAttr($value)
        {
            $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
            return $status[$value];
        }

        // email查询
        public function scopeEmail($query,$email='')
        {
            $query->where('email',$email);
        }

        // status查询
        public function scopeStatus($query)
        {
            $query->where('status',2);
        }
        
         // 全局查询范围
         protected static function base($query)
         {
            $query->where('status',2);
         } */

    }
?>